package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.Record;
	import engine.logic.LogSendLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;

	/**
	 * 帮派上香 
	 * @author liuel
	 * 
	 */	
	public class SacredLogic
	{
		private static var _self			:SacredLogic;
		private var 	_ui					:SacredUI;
		private var 	_model				:ModelLocator;
		private var	_bingManage			:BindManage;
		
		private var    _vipindex			:int 			= -1;
		private var 	_currVipLevel		:int=0;
		private var 	_state				:int;
		private var    _SacredVoArr		:Vector.<SacredVo>;
		private var    _date				:Date;
		
		private var _blindFlag:int = -1;
		private const SACRED_TYPE:int		=	1;
		private var _recordArr:Array;
		
		public static function getInstance():SacredLogic
		{
			if(_self == null)
			{
				_self = new SacredLogic();
				
			}
			return _self;
		}
		public function SacredLogic()
		{
		}
		public function Init() : void
		{
			_date	= new Date();
			_ui		= SacredUI.getInstance();
			_bingManage	=BindManage.getInstance();
			_model	= ModelLocator.getInstance();
			_SacredVoArr	= new Vector.<SacredVo>();
			//接受打开UI
			_model	.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILD_DONATE),openUI);
			_model	.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILD_OTHERS_DONATE),updateSacred);
		}
		private function initUI():void
		{
		
		}
		private function bind():void
		{
			if(_vipindex == -1)
			{
				_vipindex = _bingManage.BindProp(_model.scene.Role,"VIPLevel",this,"getVipLevel");
			}
		}
		private function unBind():void
		{
			if(_vipindex != -1)
			{
				_vipindex=_bingManage.UnbindProp(_model.scene.Role,"VIPLevel",_vipindex);
			}
			if(_blindFlag!=-1)
			{
				_blindFlag= _bingManage.UnbindRecord("FakeVipRec", _blindFlag);
			}
		}
		public function	BeforeShow():void
		{
//			_ui.mc1=ResManager.GetRess("CustomControl_017") as MovieClip;
//			_ui.windowBG.addChild(_ui.mc1);
			_ui.label1.htmlText = StaticIni.getSwfLangStr("UIString_1000116");
			_ui.label2.htmlText = StaticIni.getSwfLangStr("UIString_1000117");
			_ui.label3.htmlText = StaticIni.getSwfLangStr("UIString_1000118");
			
			addEvent();
			bind();
			
		}
		public function	AfterHide():void
		{
			
			_ui.textLabel.htmlText ="";
			_SacredVoArr	= new Vector.<SacredVo>();
			removeEvent();
			_recordArr = [];
			unBind();
		}
		private function addEvent():void
		{
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
			_ui.btn1.addEventListener(MouseEvent.CLICK,			onClickBtn1);
			_ui.btn2.addEventListener(MouseEvent.CLICK,			onClickBtn2);
			_ui.btn3.addEventListener(MouseEvent.CLICK,			onClickBtn3);
			
			_ui.planel1.addEventListener(MouseEvent.MOUSE_OVER,	onOverBtn1);
			_ui.planel1.addEventListener(MouseEvent.MOUSE_OUT,	onHideTips);
			_ui.planel2.addEventListener(MouseEvent.MOUSE_OVER,	onOverBtn2);
			_ui.planel2.addEventListener(MouseEvent.MOUSE_OUT,	onHideTips);
			_ui.planel3.addEventListener(MouseEvent.MOUSE_OVER,	onOverBtn3);
			_ui.planel3.addEventListener(MouseEvent.MOUSE_OUT,	onHideTips);
		}
		private function removeEvent():void
		{
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
			_ui.btn1.removeEventListener(MouseEvent.CLICK,		onClickBtn1);
			_ui.btn2.removeEventListener(MouseEvent.CLICK,		onClickBtn2);
			_ui.btn3.removeEventListener(MouseEvent.CLICK,		onClickBtn3);
			
			_ui.btn1.removeEventListener(MouseEvent.MOUSE_OVER,	onOverBtn1);
			_ui.btn1.removeEventListener(MouseEvent.MOUSE_OUT,	onHideTips);
			_ui.btn2.removeEventListener(MouseEvent.MOUSE_OVER,	onOverBtn2);
			_ui.btn2.removeEventListener(MouseEvent.MOUSE_OUT,	onHideTips);
			_ui.btn3.removeEventListener(MouseEvent.MOUSE_OVER,	onOverBtn3);
			_ui.btn3.removeEventListener(MouseEvent.MOUSE_OUT,	onHideTips);
		}
		public function set getVipLevel(val:int):void
		{
			VipLevel = val;
			updateUI();
		}
		public function set VipLevel(val:int):void
		{
			_currVipLevel = val;
		}
		public function get VipLevel():int
		{
			return _currVipLevel;
		}
		private function openUI(e:CustomMsgEvent=null):void
		{
			
			_ui.levellabel.text = e.pList[0]+"";
			_ui.currnum	  .text =  e.pList[1]+"/"+e.pList[2]+"";
			_state  =e.pList[3];
			if(_ui.created == false)
			{
				_ui.showOrHide();
			}
			updateUI();
		}
		private function updateSacred(e:CustomMsgEvent):void
		{
			if(!_ui.created)
				return;
			var arr:Array =e.pList;
			_ui.levellabel.text = arr.shift()+"";
			var currVal:int=arr.shift();
			var totalVal:int=arr.shift();
			_ui.currnum	  .text =  currVal+"/"+totalVal+""
			saveSacredVo(arr);
			ShowStr()
		}
		private function updateUI():void
		{
			//取到本地
			_recordArr = new Array();
			var _record:Record = _model.scene.GetRecordEx("FakeVipRec");
			var _arr:Array = _record.recArr;
			for(var i:String in _arr){
				if(_arr[i][0] == SACRED_TYPE){
					_recordArr.push(_arr[i][0], _arr[i][1]);
				}
			}
			if(_recordArr.length <= 0)
			{
				switch(_state)
				{
					case 1:
						_ui.btn1.enabled = false;
						_ui.btn2.enabled = false;
						_ui.btn3.enabled = false;
						break;
					case 0:
						byVipUpdteUI();
						break;
				}
				return;
			}
			//绑定表格
			if(_blindFlag!=-1)
			{
				_blindFlag= _bingManage.UnbindRecord("FakeVipRec", _blindFlag);
			}
			_blindFlag= _bingManage.BindRecord(_model.scene,"FakeVipRec",onaddPerHandle,onremovePerHandle);
		}
		
		private function byVipUpdteUI():void
		{
			switch(_currVipLevel)
			{
				case 0:
					_ui.btn1.enabled = true;
					_ui.btn2.enabled = false;
					_ui.btn3.enabled = false;
					break;
				case 1:
					_ui.btn1.enabled = true;
					_ui.btn2.enabled = true;
					_ui.btn3.enabled = false;
					break;
				default:
					_ui.btn1.enabled = true;
					_ui.btn2.enabled = true;
					_ui.btn3.enabled = true;
					break;
			}
		}
		
		private function onaddPerHandle(PropertiesName:String,PropertiesValue:Array,data:*):void
		{
			if(PropertiesValue[0] == SACRED_TYPE){
				_recordArr[1] = PropertiesValue[1];
				if(PropertiesValue[1] > 0){
					_ui.btn1.enabled = true;
					_ui.btn2.enabled = true;
					_ui.btn3.enabled = true;
				} else {
					_ui.btn1.enabled = false;
					_ui.btn2.enabled = false;
					_ui.btn3.enabled = false;
					
					switch(_state)
					{
						case 1:
							_ui.btn1.enabled = false;
							_ui.btn2.enabled = false;
							_ui.btn3.enabled = false;
							break;
						case 0:
							byVipUpdteUI();
							break;
					}
				}
			}
		}
		
		private function onremovePerHandle(PropertiesName:String,PropertiesValue:Array,data:*):void
		{
			
		}
		
		/**
		 * 保存消息 
		 * @param arr
		 * @return 
		 * 
		 */		
		private function saveSacredVo(arr:Array):void
		{
			var vo:SacredVo ;
			for(var i:int=0;i<arr.length;i+=4)
			{
				vo =new SacredVo(arr[i],arr[i+1],arr[i+2],arr[i+3]);
				check(vo);
			}

		}
		private function check(v:SacredVo):void
		{
			for each(var i:SacredVo in _SacredVoArr)
			{
				if(i.name == v.name && i.time == v.time)
				{
					return ;
				}
			}
			_SacredVoArr.splice(0,0,v);
		}
		private function ShowStr():void
		{
			var str:String="";
			_ui.textLabel.htmlText = "";
			for(var i:String in _SacredVoArr)
			{
				str+=byVoSetStr(_SacredVoArr[i])+"<br>";
				_ui.textLabel.htmlText =str;
			}
		}
		private function  byVoSetStr(vo:SacredVo):String
		{
			var time:String = vo.time;
			var typename:String;
			var strID:String;
			switch(vo.type)
			{
				case 1:
					strID ="UIString_1000119";
					typename =StaticIni.getSwfLangStr("UIString_1000122");
					break;
				case 2:
					strID ="UIString_1000120";
					typename =StaticIni.getSwfLangStr("UIString_1000123");
					break;
				case 3:
					strID ="UIString_1000121";
					typename =StaticIni.getSwfLangStr("UIString_1000124");
					break;
			}
			var str:String = StaticIni.getSwfLangStrVar(strID,[time,vo.name,typename,vo.num]);
			return str;
		}
		private function initTime(time:Number):String
		{
			var str:String;
			_date.time = time*1000;
			var hours:int= _date.hoursUTC;
			var minutes :int= _date.minutesUTC;
			if(hours>0)
			{
				str = hours+StaticIni.getSwfLangStr("UIString_1000139");
			}
			else if(minutes<30)
			{
				str = StaticIni.getSwfLangStr("UIString_1000138");
			}
			else if(minutes>=30)
			{
				str = 1+StaticIni.getSwfLangStr("UIString_1000139");
			}
			return str;
		}

		
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		private function onClose(e:NxWindowEvent):void
		{
			_ui.hide();
		}
		private function onClickBtn1(e:MouseEvent):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GUILD_DONATE,1);
			_ui.mc1.gotoAndPlay(1);
		}
		private function onClickBtn2(e:MouseEvent):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GUILD_DONATE,2);
			LogSendLogic.getInstance().btnClickNumSend(22834);
			_ui.mc2.gotoAndPlay(1);
		}
		private function onClickBtn3(e:MouseEvent):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GUILD_DONATE,3);
			LogSendLogic.getInstance().btnClickNumSend(22835);
			_ui.mc3.gotoAndPlay(1);
		}
		private function onOverBtn1(e:MouseEvent):void
		{
			if(_state == 1)
			{
				if(!_ui.btn3.enabled){
					TipsMainLogic.instance.showString( StaticIni.getSwfLangStr("UIString_1000134"));
				}
			}
		}
		private function onOverBtn2(e:MouseEvent):void
		{
			var str:String;
			if(_state ==1)
			{
				if(!_ui.btn3.enabled){
					str = StaticIni.getSwfLangStr("UIString_1000134");
				}
			}
			else
			{
				if(_ui.btn2.mouseEnabled)
				{
					return;
				}
				str = StaticIni.getSwfLangStr("UIString_1000135");
			}
			TipsMainLogic.instance.showString(str);
			
		}
		private function onOverBtn3(e:MouseEvent):void
		{
			var str:String;
			if(_state ==1)
			{
				if(!_ui.btn3.enabled){
					str = StaticIni.getSwfLangStr("UIString_1000134");
				}
			}
			else
			{
				if(_ui.btn3.mouseEnabled)
				{
					return;
				}
				str = StaticIni.getSwfLangStr("UIString_1000136");
			}
			TipsMainLogic.instance.showString(str);
			
		}
		private function onHideTips(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
	}
}
class SacredVo
{
	public	 var time	:String;		//时间
	public  var name	:String;	//名字
	public	 var type	:int;		//上香类型
	public  var num	:int;		//获得的声望
	public function SacredVo(time:String,name:String,type:int,num:int):void
	{
		this.time = time;
		this.name = name;
		this.type = type;
		this.num  = num;
	}
}